<?php
namespace App\Libraries\Small;

class WxSmallService {

    const APPID = 'wxe66483d456d1bc4e';
    const SECRET = '3a61d37e5c3f2c361fa2663d0b9f4047';



    static public function get_access_token(){
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.self::APPID.'&secret='.self::SECRET;

        $access_token = self::get($url);
        if($access_token['code'] == 200){
            return ['code' => 200, 'message' => '获取成功','data'=>$access_token['data']];

        }else{
            return array( 'code' => 400, 'message' => '获取失败' );
        }
    }


    static public function get( $url, $http_opts = null, $is_transcoding = true ) {

        if (!isset($url) || empty($url)) {
            return array( 'code' => 400, 'message' => '缺少请求链接' );
        }

        $curl_handler = curl_init();

        $options = array(
            CURLOPT_URL             => $url,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_CONNECTTIMEOUT  => 15,
            CURLOPT_HEADER			=> false,
            CURLOPT_USERAGENT       => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36',
        );

        if (is_array($http_opts)) {
            foreach ($http_opts as $key => $value){
                $options[$key] = $value;
            }
        }

        curl_setopt_array($curl_handler, $options);
        $curl_result = curl_exec($curl_handler);
        $curl_http_status = curl_getinfo($curl_handler,CURLINFO_HTTP_CODE);
        $curl_http_info = curl_getinfo($curl_handler);
        if ($curl_result === false) {
            $error = curl_error($curl_handler);
            curl_close($curl_handler);
            return array('code' => $curl_http_status, 'message' => $error,'http_info' => $curl_http_info);

        }
        if ($is_transcoding) {
            $encode = mb_detect_encoding($curl_result, array('ASCII', 'UTF-8','GB2312', 'GBK', 'BIG5'));
            if ($encode != 'UTF-8') {
                $curl_result = iconv($encode, 'UTF-8', $curl_result);
            }
        }

        $result = json_decode($curl_result,true);
        if (is_null($result) || empty($result)) {
            $result = $curl_result;
        }

        curl_close($curl_handler);

        return array('code' => $curl_http_status, 'data' => $result,'http_info' => $curl_http_info);

    }

    /**
     * post请求
     * GuzzleHttp\Client库的post请求必须使用数组，微信支付post请求数据是xml格式
     * @param string $url 接口地址
     * @param string $data 请求数据
     * @param null $http_opts
     * @return array
     */
    static public function postRequest( $url, $data, $http_opts = null ) {

        if (!isset($url) || empty($url)) {
            return array( 'code' => 400, 'message' => '缺少请求链接' );
        }
        if (!isset($data)) {
            return array( 'code' => 400 ,'message' => '缺少请求参数');
        }

        //解析链接，判断请求协议
        //$parse_url_array = parse_url($url);

        $curl_handler = curl_init();

        $options = array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_CONNECTTIMEOUT => 15,
            CURLOPT_HEADER	 => false,
            CURLOPT_POST => TRUE,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_SSL_VERIFYHOST => 2,
            CURLOPT_POSTFIELDS => $data
        );

        if (is_array($http_opts)) {
            foreach ($http_opts as $key => $value) {
                $options[$key] = $value;
            }
        }

        curl_setopt_array($curl_handler, $options);
        $curl_result = curl_exec($curl_handler); //获取URL站点内容 并打印出来
        $curl_http_status = curl_getinfo($curl_handler,CURLINFO_HTTP_CODE); //获取最后一次收到的HTTP代码
        $curl_http_info = curl_getinfo($curl_handler);

        if ($curl_result == false) {
            $error = curl_error($curl_handler);
            curl_close($curl_handler);
            return array('code' => $curl_http_status, 'message' => $error);
        }
        curl_close($curl_handler);

       /* $encode = mb_detect_encoding($curl_result, array('ASCII', 'UTF-8','GB2312', 'GBK', 'BIG5')); //进行编码识别
        if ($encode != 'UTF-8') {
            $curl_result = iconv($encode, 'UTF-8', $curl_result);
        }*/

        $result = json_decode($curl_result, true);

        if (is_null($result)) {
            $result = $curl_result;
        }

        return array('code' => $curl_http_status, 'message' => 'ok', 'data' => $result);

    }


    static function api_notice_increment($url, $data){
        $ch = curl_init();
        $header = ["Accept-Charset: utf-8"];
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return false;
        }else{
            return $tmpInfo;
        }
    }
}